/**
 * 
 */
package zhihu;

import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.wixc.entity.Model;
import com.wixc.entity.User;
import com.wixc.util.JdbcUtils;
import com.wixc.util.LoginUtil;
import com.wixc.util.MyStringUtils;


/**
 * @author weichuanxi
 * @date 2016年3月18日
 */
public class ZhiHu {
	static ArrayList<Model> zhihulist = new ArrayList<Model>();

	public void spider(User user, int index) throws FileNotFoundException, SQLException {

		ZhiHuLoginUtil Loginutil = new ZhiHuLoginUtil();
		LoginUtil util = new LoginUtil();
		boolean status = Loginutil.Login(util, user.getUsername(), user.getPassword());
		if (status) {// 登陆成功
			String html = "";
			// 第1-20通过get得到
			if (index<20) {
				try {
					html = util.Get("https://www.zhihu.com/#signin");
					SpiderInfo_Get(util, html);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			// 21之后post
			try {
				Map<String, String> header = new LinkedHashMap<String, String>();
				html = util.Get("https://www.zhihu.com/#signin");
				Document doc = Jsoup.parse(html);
				String _xsrf = doc.select("input[name=_xsrf]").attr("value");
				header.put("referer", "https://www.zhihu.com/");
				header.put("accept", "*/*");
				header.put("accept-encoding", "gzip, deflate");
				header.put("accept-language", "zh-CN,zh;q=0.8");
				header.put("x-requested-with", "XMLHttpRequest");
				header.put("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
				header.put(":host", "www.zhihu.com");
				header.put(":method", "POST");
				header.put(":path:", "/node/TopStory2FeedList");
				header.put(":scheme", "https");
				header.put(":version", "HTTP/1.1");
				Map<String, String> postdata = new LinkedHashMap<String, String>();
				postdata.put("params", "{\"offset\":" + index + ",\"start\":" + index + "}");
				postdata.put("method", "next");
				postdata.put("_xsrf", _xsrf);

				html = util.Post("https://www.zhihu.com/node/TopStory2FeedList", header, postdata);
				SpiderInfo_Post(util, html);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			JdbcUtils jdbcUtils = new JdbcUtils();
			@SuppressWarnings("static-access")
			Connection connection = jdbcUtils.getConnection();
			
			String sql = "insert into userbean (id, name, password) values (?,?, ?)";
			PreparedStatement ps = null;
			try {
				ps = (PreparedStatement) connection.prepareStatement(sql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			final int batchSize = 15;
			int count = 0;

			for (Model m : zhihulist) {

					ps.setString(1, 0 + "");
					ps.setString(2, m.getName());
					ps.setString(3, m.getDetail());
					ps.addBatch();
					if (++count % batchSize == 0) {
						ps.executeBatch();
			}
			
			}
		}
	}

	@SuppressWarnings("unchecked")
	private void SpiderInfo_Post(LoginUtil util, String html) {
		Map<String, Object> map = MyStringUtils.json2Map(html);
		List<String> lists = (List<String>) map.get("msg");
		for (String list : lists) {
			html = "<html><body>" + list.toString() + "</body></html>";
			Document doc = Jsoup.parse(html);
			Elements allUrl = doc.select("a");
			String answerUrl = "";
			for (Element element : allUrl) {
				if (element.text().contains("显示全部")) {
					answerUrl = element.attr("href");
					continue;
				}
			}
			if (!answerUrl.startsWith("http")) {
				answerUrl = MyStringUtils.tidyUrl("https://www.zhihu.com/#signin", answerUrl);
			}
			String dhtml = util.Get(answerUrl);
			Document ddoc = Jsoup.parse(dhtml);
			String question = doc.select("h2").text();
			String answer = ddoc.select("div.zm-item-rich-text.js-collapse-body > div.zm-editable-content.clearfix").html();
			Model zhihu = new Model(0, question, answer);
			zhihulist.add(zhihu);
		}
	}

	/**
	 * @param util
	 * @param html
	 */
	private void SpiderInfo_Get(LoginUtil util, String html) {

		Document doc = Jsoup.parse(html);
		Elements e = doc.select("#js-home-feed-list");
		for (Element element : e) {
			for (int i = 1; i < 22; i++) {
				String question = element.select("#feed-" + i).select("h2").text();
				String url = element.select("#feed-" + i).select("div.zm-item-answer-detail > div.zm-item-rich-text.js-collapse-body > div > a")
						.attr("href");
				if (!url.startsWith("http")) {
					url = MyStringUtils.tidyUrl("https://www.zhihu.com/#signin", url);
				}
				String dhtml = util.Get(url);
				Document ddoc = Jsoup.parse(dhtml);
				String answer = ddoc.select("div.zm-item-rich-text.js-collapse-body > div.zm-editable-content.clearfix").html();

				Model zhihu = new Model();
				zhihu.setName(question);
				zhihu.setDetail(answer);
				zhihulist.add(zhihu);
			}
		}
	}

	public static void main(String[] args) throws FileNotFoundException {
		ZhiHu_Run();
	}
	public static void ZhiHu_Run() throws FileNotFoundException{
		String name = "18600909436@163.com";
		String password = "wcx3412232355";
		User user = new User();
		user.setUsername(name);
		user.setPassword(password);
		// 用户帐号密码，开始下标20的倍数
		for (int i = 0; i < 6; i++) {
			try {
				new ZhiHu().spider(user, 20*i);
			} catch (SQLException e1) {
				continue;
			}
		}
		try {
			JdbcUtils.save(zhihulist, "insert into userbean (id,name, password) values (?,?, ?)");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
